<template>
  <div class="topbar">
    <div class="topbar-nav wrap">
      <div class="left">欢迎来到叩丁狼积分商城</div>
      <div class="right">
        <ul>
          <li>
            <img class="avarimg" :src="userInfo.headImg" alt="" />
            用户名：游客
          </li>
          <li>我的积分：{{ userInfo.coin }}</li>
          <li>获取积分</li>
          <li>叩丁狼官网</li>
          <li class="login-btn" @click="showModalFn(true)" v-if="!isLogined">
            登录
          </li>
          <li class="cart-btn" v-if="isLogined">
            <img src="../assets/img/cart.png" alt="" />
            <span>购物车</span>
            <strong>{{ cartTotal }}</strong>
          </li>
        </ul>
      </div>
    </div>
  </div>
</template>

<script>
import { mapMutations, mapState, mapActions } from "vuex";
import { WeixinLoginAPI } from "../request/api";
export default {
  data() {
    return {};
  },

  computed: {
    ...mapState({
      isLogined: (state) => state.loginStatus.isLogined,
      // 用户信息
      userInfo: (state) => state.userInfo.userInfo,
      cartTotal: (state) => state.userInfo.cartTotal,
    }),
  },
  methods: {
    ...mapMutations({
      // 登录框
      showModalFn: "showModal/showModalFn",
      // 购物车
      changeIsLogined: "loginStatus/changeIsLogined",
      // 初始化用户信息
      initUserInfo: "userInfo/initUserInfo",
    }),
    ...mapActions({
      // 提示框
      asyncChangeToast: "showToast/asyncChangeToast",
      //  修改用户信息
      asyncChangeUserInfo: "userInfo/asyncChangeUserInfo",
    }),
  },
  created() {
    // undefined  在执行created时候，可能router对象还没有初始完成
    // console.log("code", this.$route.query.code);
    // 在router初始化完毕后再获取
    this.$router.onReady(async () => {
      // console.log("onReady-code", this.$route.query.code);
      let pathCode = this.$route.query.code;
      // 是否有code
      if (pathCode) {
        let res = await WeixinLoginAPI({
          code: pathCode,
        });
        // 分三种情况处理
        if (res.code == 0) {
          // 登录成功
          // 1-提示登录成功
          this.asyncChangeToast({
            isShow: true,
            title: "微信扫码登录成功",
            type: "success",
          });
          // 2-存储token
          localStorage.setItem("x-auth-token", res["x-auth-token"]);
          // 3-切换购物车
          this.changeIsLogined(true);
          // 4-删除code 防止触发400
          this.$router.push(this.$route.path);
          // 更新用户信息
          this.asyncChangeUserInfo();
        } else if (res.code == 400) {
          // 二维码超时2min  code已经使用过了  再次发送  code错误
          // 1-提示用户重新扫码
          this.asyncChangeToast({
            isShow: true,
            title: "请重新扫码",
            type: "error",
          });
          // 2-显示登录框
          this.showModalFn(true);
          // 清除code
          // this.$router.push(this.$route.path);
        }
        if (res.code == 407) {
          // 1-提示要绑定手机号登录
          this.asyncChangeToast({
            isShow: true,
            title: "请绑定手机号",
            type: "error",
          });
          // 2-显示登录框
          this.showModalFn(true);
          // 3-存储绑定凭证uuid
          localStorage.setItem("uuid", res.uuid);
          // 4-删除code 防止触发400
          this.$router.push(this.$route.path);
        }
      } else {
        // ！！！如果不是微信登录进来的，就会执行这里的代码
        let token = localStorage.getItem("x-auth-token");
        this.changeIsLogined(Boolean(token));
        // 判断是否登录成功，更新用户数据
        if (token) {
          // 已登录
          // 根据是否有token刷新用户信息
          console.log("1111");
          this.asyncChangeUserInfo();
        } else {
          //未登录
          this.initUserInfo();
        }
      }
    });
  },
  // watch: {
  //   "$route.path": {
  //     handler(newVal, oldVal) {
  //       let token = localStorage.getItem("x-auth-token");
  //       // if (token) {
  //       //   this.changeIsLogined(true);
  //       // } else {
  //       //   this.changeIsLogined(false);
  //       // }
  //       this.changeIsLogined(Boolean(token));
  //     },
  //   },
  // },
};
</script>

<style lang="less" scoped>
.topbar {
  width: 100%;
  height: 40px;
  line-height: 40px;
  background: #242b39;
  font-size: 14px;
  color: #fffefe;
  .topbar-nav {
    display: flex;
    justify-content: space-between;
    .right {
      ul {
        display: flex;
      }
      li {
        margin-right: 20px;
        &.login-btn {
          width: 124px;
          text-align: center;
          background: #0a328e;
          cursor: pointer;
        }
        &.cart-btn {
          width: 124px;
          height: 40px;
          background: #0a328e;
          text-align: center;
          img {
            width: 20px;
            height: 19px;
            vertical-align: middle;
          }
          span {
            margin: 0 8px;
          }
          strong {
            width: 22px;
            height: 22px;
            line-height: 22px;
            display: inline-block;
            border-radius: 50%;
            background-color: #fd604d;
          }
        }
        .avarimg {
          width: 23px;
          height: 23px;
          vertical-align: middle;
          border-radius: 50%;
        }
      }
    }
  }
}
</style>
